//
//  NSString+Extension.m
//  tuotianClient
//
//  Created by alankong on 15/7/21.
//  Copyright (c) 2015年 TuoTianSuDai. All rights reserved.
//

#import "NSString+Extension.h"
#import <CommonCrypto/CommonDigest.h>

#define kMoneyCaption @"¥ " //￥半角，¥全角
#define CC_MD5_DIGEST_LENGTH 16

@implementation NSString (Extension)

- (NSString *)toMD5String
{
    const char *cStr = [self UTF8String];
    unsigned char result[CC_MD5_DIGEST_LENGTH];
    CC_MD5(cStr, (CC_LONG)strlen(cStr), result);
    
    NSString *resultStr = [NSString stringWithFormat:@"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
                           result[0], result[1], result[2], result[3],
                           result[4], result[5], result[6], result[7],
                           result[8], result[9], result[10], result[11],
                           result[12], result[13], result[14], result[15]
                           ];
    return [resultStr lowercaseString];
}

- (CGSize)stringSizeWithFont:(UIFont *)font
{
    NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
                                font, NSFontAttributeName,nil];
    CGSize labelSize = [self sizeWithAttributes:attributes];
    return labelSize;
}

- (CGFloat)stringHeightWithFont:(UIFont *)font withWidth:(CGFloat)width
{
    NSDictionary *attributes = @{NSFontAttributeName:font};
    CGRect stringRect = [self boundingRectWithSize:CGSizeMake(width, MAXFLOAT)
                                          options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
                                       attributes:attributes context:nil];
    return stringRect.size.height+5;//加5高度，最后一行显示不出来(加1就可以)
}

- (CGFloat)getStringHeightWithFont:(UIFont *)font withWidth:(CGFloat)width
{
    NSDictionary *attributes = @{NSFontAttributeName:font};
    CGRect stringRect = [self boundingRectWithSize:CGSizeMake(width, CGFLOAT_MAX)
                                           options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
                                        attributes:attributes context:nil];
    
    CGFloat widthResult = stringRect.size.width;
    if(widthResult - width >= FLOATZERO)
    {
        widthResult = width;
    }
    stringRect.size.width = widthResult;
    
    return stringRect.size.height+5;//加5高度，最后一行显示不出来(加1就可以)
}

- (NSString*)concat:(NSString*)string
{
    if (string.length <= 0) {
        return self;
    }
    
    return [NSString stringWithFormat:@"%@%@", self, string];
}

- (NSString*)concatPath:(NSString*)pathString
{
    if ([pathString hasPrefix:@"http"]) {
        return pathString;
    }
    
    if ([self hasSuffix:@"/"] && [pathString hasPrefix:@"/"]) {
        pathString = [pathString substringFromIndex:1];
        return [self concat:pathString];
    } else if (![self hasSuffix:@"/"] && ![pathString hasPrefix:@"/"]) {
        return [NSString stringWithFormat:@"%@/%@", self, pathString];
    }
    
    return [self concat:pathString];
}

- (NSString*)concatQueryParams:(NSString*)queryString
{
    NSRange range = [self rangeOfString:@"?"];
    if (range.length > 0) {
        return [[self concat:@"&"] concat:queryString];
    }
    
    return [[self concat:@"?"] concat:queryString];
}

-(NSDictionary*)paramsFromQueryString:(NSString*)queryString
{
    NSString* string = [queryString stringByReplacingOccurrencesOfString:@"?" withString:@""];
    NSMutableDictionary *dict=[NSMutableDictionary dictionary];
    
    NSArray *array=[string componentsSeparatedByString:@"&"];
    
    for (NSString* param in array) {
        NSArray* arrParam = [param componentsSeparatedByString:@"="];
        if (arrParam.count > 1) {
            [dict setObject:arrParam[1] forKey:arrParam[0]];
        }
    }
    
    return dict;
}

- (BOOL)isEqualToStringCaseInsensitive:(NSString*)string
{
    return [self caseInsensitiveCompare:string] == NSOrderedSame;
}

- (NSString*)trimString
{
    return [self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
}

- (BOOL)hasSubstring:(NSString*)string
{
    NSRange range = [self rangeOfString:string];
    return range.length > 0;
}
@end
